package com.googlecode.smartgwtmavenplugin;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * A little polymorphic Enum meant to model the files that can be downloaded with the various SmartGWT licenses, and the content of 
 * each.  The bulk of change from one SmartGWT version to the next should hopefully be handled right here (along with the files at 
 * src/main/resources).
 */
public enum Archive {

	LGPL() {
		protected String getUrlPattern() {
			return "LGPL/{1}/smartgwt-{0}.zip";
		}
		public Map<String, String> getZipEntries() {
			Map<String, String> overridden = new HashMap<String, String>();
			overridden.put("*/doc/**", "/doc/**");
			overridden.put("*/smartgwt.jar", "smartgwt-lgpl.jar");
			overridden.put("*/smartgwt-skins.jar", "smartgwt-skins.jar");		
			return overridden;
		}
	}, EVAL() {
		protected String getUrlPattern() {
			return "EnterpriseEval/{1}/smartgwtee-{0}.zip";
		}
		public Map<String, String> getZipEntries() {
			Map<String, String> overridden = new HashMap<String, String>();
			overridden.putAll(ENTERPRISE.getZipEntries());
			overridden.put("*/lib/messaging.jar", "smartgwt-messaging.jar");
			overridden.put("*/lib/isomorphic_js_parser.jar", "isomorphic_js_parser.jar");
			overridden.put("*/lib/isomorphic_realtime_messaging.jar", "isomorphic_messaging.jar");
			overridden.put("*/lib/drawing.jar", "smartgwt-drawing.jar");
			overridden.put("*/lib/analytics.jar", "smartgwt-analytics.jar");
			overridden.put("*/lib/smartgwtee.jar", "smartgwt-eval.jar");
			return overridden;
		}
	}, PRO() {
		protected String getUrlPattern() {
			return "/Pro/{1}/smartgwtpro-{0}.zip";
		}
		public Map<String, String> getZipEntries() {
			entries.put("*/lib/smartgwtpro.jar", "smartgwt-pro.jar");
			return entries;
		}
	}, POWER() {
		protected String getUrlPattern() {
			return "PowerEdition/{1}/smartgwtpower-{0}.zip";
		}
		public Map<String, String> getZipEntries() {
			entries.put("*/lib/smartgwtpower.jar", "smartgwt-power.jar");
			return entries;
		}
	}, ENTERPRISE() {
		protected String getUrlPattern() {
			return "Enterprise/{1}/smartgwtee-{0}.zip";
		}
		public Map<String, String> getZipEntries() {
			entries.put("*/lib/smartgwtee.jar", "smartgwt-enterprise.jar");
			return entries;
		}
	}, MESSAGING() {
		protected String getUrlPattern() {
			return "RealtimeMessagingModule/{1}/messaging.zip";
		} 
		public Map<String, String> getZipEntries() {
			Map<String, String> overridden = new HashMap<String, String>();
			overridden.put("messaging.jar", "smartgwt-messaging.jar");
			overridden.put("isomorphic_js_parser.jar", "isomorphic_js_parser.jar");
			overridden.put("isomorphic_realtime_messaging.jar", "isomorphic_messaging.jar");
			return overridden;
		}
	}, ANALYTICS() {
		protected String getUrlPattern() {
			return "AnalyticsModule/{1}/analytics.jar";
		}
		public Map<String, String> getZipEntries() {
			return new HashMap<String, String>();
		}
		public String getContentType() {
			return CONTENTTYPE_JAR;
		}
	}, DRAWING() {
		protected String getUrlPattern() {
			return "AnalyticsModule/{1}/drawing.jar";
		}
		public Map<String, String> getZipEntries() {
			return new HashMap<String, String>();
		}
		public String getContentType() {
			return CONTENTTYPE_JAR;
		}
	};

	public static String BUILD = "2.5p";
	
	private static String CONTENTTYPE_JAR = "application/java-archive";
	private static String CONTENTTYPE_ZIP = "application/zip";
	
	Map<String, String> entries = new HashMap<String, String>();
	
	private Archive() {
		entries.put("*/doc/**", "/doc/**");
		entries.put("*/lib/isomorphic_assembly.jar", "isomorphic_assembly.jar");
		entries.put("*/lib/isomorphic_compression.jar", "isomorphic_compression.jar");
		entries.put("*/lib/isomorphic_core_rpc.jar", "isomorphic_core_rpc.jar");
		entries.put("*/lib/isomorphic_hibernate.jar", "isomorphic_hibernate.jar");
		entries.put("*/lib/isomorphic_jpa.jar", "isomorphic_jpa.jar");
		entries.put("*/lib/isomorphic_spring.jar", "isomorphic_spring.jar");
		entries.put("*/lib/isomorphic_sql.jar", "isomorphic_sql.jar");
		entries.put("*/lib/isomorphic_struts.jar", "isomorphic_struts.jar");
		entries.put("*/lib/isomorphic_tools.jar", "isomorphic_tools.jar");
		entries.put("*/lib/smartgwt-skins.jar", "smartgwt-skins.jar");	
		entries.put("*/lib/isc-jakarta-oro-2.0.6.jar", "isc-jakarta-oro.jar");
		
		//only in 2.5 for pro+?
		entries.put("*/lib/smartgwt.jar", "smartgwt-core.jar");
	}
	
	public String getRelativeUrl(String build, String date) {
		MessageFormat fmt = new MessageFormat(getUrlPattern());
		return fmt.format(new String[]{build, date});
	}
	
	public String getContentType() {
		return CONTENTTYPE_ZIP;
	}
	
	public String getLocalFileName() {
		String ext = getZipEntries().isEmpty() ? ".jar" : ".zip";
		return "smartgwt-".concat(toString().toLowerCase().concat(ext));
	}

	protected abstract String getUrlPattern();
	public abstract Map<String, String> getZipEntries();
	
}